package com.car.account.controller;


import cn.hutool.core.bean.BeanUtil;
import com.car.account.controller.form.userForm.*;
import com.car.account.dto.userDto.UserDto;
import com.car.account.dto.userDto.UserIdRequest;
import com.car.account.dto.userDto.UserInfoByIdDto;
import com.car.account.entity.User;
import com.car.account.exception.AccountException;
import com.car.account.param.userParam.UserRealNameParam;
import com.car.account.service.UserService;
import com.car.commons.core.model.Result;
import com.car.commons.core.util.JwtUtil;
import com.car.commons.web.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户账号")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/userPassLogin")
    @ApiOperation(value = "用户密码登录")
    public Result userPassLogin(@RequestBody UserPassLoginForm form) throws Exception{
        Integer userId = userService.userPassLogin(form);
        Map<String, Object> map = new HashMap<>();
        map.put("userId",userId);
        String token = JwtUtil.createUserToken(map, 24 * 60);
        WebUtil.getResponse().setHeader("car-token",token);
        WebUtil.getResponse().setHeader("Access-Control-Expose-Headers","car-token");
        return Result.getResult().setMsg("用户登录成功");
    }

    @GetMapping("/sendUserLoginCode")
    @ApiOperation(value = "用户发送登录验证码")
    public Result sendUserLoginCode(String userPhone) throws Exception{
        userService.sendUserLoginCode(userPhone);
        return Result.getResult().setMsg("登录短信验证码发送成功");
    }

    @PostMapping("/userCodeLogin")
    @ApiOperation(value = "用户验证码登录")
    public Result userCodeLogin(@RequestBody UserCodeLoginForm form) throws Exception{
        Integer userId = userService.userCodeLogin(form);
        Map<String, Object> map = new HashMap<>();
        map.put("userId",userId);
        String token = JwtUtil.createUserToken(map, 24 * 60);
        WebUtil.getResponse().setHeader("car-token",token);
        WebUtil.getResponse().setHeader("Access-Control-Expose-Headers","car-token");
        return Result.getResult().setMsg("用户登录成功");
    }

    @GetMapping("/sendUserRegisterCode")
    @ApiOperation(value = "用户发送注册验证码")
    public Result sendUserRegisterCode(String userPhone) throws Exception{
        userService.sendUserRegisterCode(userPhone);
        return Result.getResult().setMsg("注册短信验证码发送成功");
    }

    @PostMapping("/userCodeRegister")
    @ApiOperation(value = "用户验证码注册")
    public Result userCodeRegister(@RequestBody UserCodeRegisterForm form) throws Exception{
        userService.userCodeRegister(form);
        return Result.getResult().setMsg("用户注册成功");
    }

    @GetMapping("/selectUserInfoDtoById")
    @ApiOperation(value = "用户根据id查询个人信息")
    public Result<UserInfoByIdDto> selectUserInfoDtoById() throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        User user = userService.getById(userId);
        UserInfoByIdDto userInfoByIdDto = BeanUtil.toBean(user, UserInfoByIdDto.class);
        return Result.getResult(UserInfoByIdDto.class).setData(userInfoByIdDto).setMsg("根据用户id查询个人信息成功");
    }

    @PostMapping("/userRealName")
    @ApiOperation(value = "用户实名认证")
    public Result userRealName(@RequestBody UserRealNameForm form) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        UserRealNameParam param = BeanUtil.toBean(form, UserRealNameParam.class);
        param.setUserId(userId);
        userService.userRealName(param);
        return Result.getResult().setMsg("用户实名认证成功");
    }

    @GetMapping("/updateUserPhoneById")
    @ApiOperation(value = "用户修改绑定手机号")
    public Result updateUserPhoneById(String userPhone) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        userService.updateUserPhoneById(userId, userPhone);
        return Result.getResult().setMsg("修改绑定手机成功");
    }

    @GetMapping("/updateUserPassById")
    @ApiOperation(value = "用户通过id修改密码")
    public Result updateUserPassById(String userPass) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        userService.updateUserPassById(userId, userPass);
        return Result.getResult().setMsg("修改密码成功");
    }

    @PostMapping("/userUpdateImage")
    @ApiOperation(value = "用户更改头像")
    public Result<String> userUpdateImage(MultipartFile mf) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        String imageName = userService.userUpdateImage(mf, userId);
        return Result.getResult(String.class).setData(imageName).setMsg("修改头像成功");
    }

    @GetMapping("/userUpdateNickname")
    @ApiOperation(value = "用户修改昵称")
    public Result<String> userUpdateNickname(String userNickname) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        String nickname = userService.userUpdateNickname(userNickname, userId);
        return Result.getResult(String.class).setData(nickname).setMsg("修改昵称成功");
    }

    /**
     * openfeign
     */
    @PostMapping("/findUserNicknameById.ty")
    @ApiOperation(value = "通过id查找用户昵称")
    public Result<String> findUserNicknameById(@RequestBody UserIdRequest request) throws Exception {
        Integer userId = request.getUserId();
        User user = userService.getById(userId);
        //判断是否存在用户
        if (user == null || user.getUserState() == 2) throw new AccountException(1000, "用户不存在");
        //判断状态
        if (user.getUserState() == 1) throw new AccountException(1002, "用户已被禁用");
        return Result.getResult(String.class).setData(user.getUserNickname()).setMsg("根据id查找用户昵称成功");
    }


    /**
     * order,openfeign
     * @param userId
     * @return
     * @throws Exception
     */
    @GetMapping("/findUserById/{userId}.ty")
    @ApiOperation(value = "通过用户id返回一个用户dto")
    public Result<UserDto> findUserById(@PathVariable("userId") Integer userId) throws Exception{
        User user = userService.getById(userId);
        if (user == null) throw new AccountException(30001,"不存在该用户信息");
        UserDto userDto = BeanUtil.toBean(user, UserDto.class);
        return Result.getResult(UserDto.class).setData(userDto).setMsg("根据Id查询用户成功");
    }

}

